<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="850" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">

			<el-form-item label="头像" prop="feng_thumb">
				<sc-upload v-model="form.avator" title="头像"></sc-upload>
			</el-form-item>
			<el-form-item label="昵称" prop="wx_name">
				<el-input v-model="form.wx_name" clearable placeholder="必填"></el-input>
			</el-form-item>
            <el-form-item label="姓名" prop="username">
				<el-input v-model="form.username" clearable></el-input>
			</el-form-item>
            <el-form-item label="手机" prop="phone">
				<el-input v-model="form.phone" clearable></el-input>
			</el-form-item>
            <el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email" clearable></el-input>
			</el-form-item>
            <el-form-item label="年龄" prop="age">
				<el-input v-model="form.age" clearable></el-input>
			</el-form-item>
            <el-form-item label="积分" prop="jifen">
				<el-input v-model="form.jifen" :disabled="true"></el-input>
			</el-form-item>
            <el-form-item label="余额" prop="balance">
				<el-input v-model="form.balance" :disabled="true"></el-input>
			</el-form-item>

            <el-form-item label="启用" prop="status">
				<el-switch v-model="form.status"></el-switch>
			</el-form-item>

		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {},
				//验证规则
				rules: {

					// title: [
					// 	{required: true, message: '请输入标题'}
					// ]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.demo.user_post.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				if(this.mode == 'edit'){
					this.form.uid = data.uid
				}
				this.form.wx_name = data.wx_name
                this.form.username = data.username
                this.form.sex = data.sex
                this.form.status = data.status
                this.form.phone = data.phone
                this.form.age = data.age
                this.form.email = data.email
                this.form.avator = data.avator
                this.form.jifen = data.jifen
                this.form.balance = data.balance
			}
		}
	}
</script>

<style>
</style>
